package main

func lengthOfLongestSubstring(s string) int {
	m := make(map[byte]struct{})
	l := 0
	max := 0
	for r := 0; r < len(s); r++ {
		c := s[r]
		if _, ok := m[c]; !ok {
			// 不存在字符 c
			m[c] = struct{}{}
			if r-l+1 > max {
				max = r - l + 1
			}
		} else {
			// shrink
			for l < r {
				if s[l] == c {
					l++
					break
				}
				delete(m, s[l])
				l++
			}
		}

	}
	return max
}

func lengthOfLongestSubstring2(s string) int {
	size := len(s)
	if size < 2 {
		return len(s)
	}
	result := 0
	m := make(map[uint8]bool) // last index
	i := 0
	for j := 0; i < size && j < size; {
		c := s[j]
		_, ok := m[c]
		if !ok { // extend sub string
			if j-i+1 > result {
				result = j - i + 1
			}
			m[c] = true
			j++
		} else {
			delete(m, s[i])
			i++
		}

	}
	return result
}

func main() {
	println(lengthOfLongestSubstring("abcabcbb"))
}
